package com.dfhc.util;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import org.apache.commons.codec.binary.Base64;

import sun.misc.BASE64Decoder;

/**
 * 3DES加密及解密工具
 * @author longseb
 *
 */
public class ThreeDESHelper {
	//
	/**
	 * 解密过程，先用Base64进行解密，然后再用3DES进行第二次解密，得到明文
	 * 
	 * @param src 明文
	 * @param key 密钥
	 * @param charSet 字符集
	 */
	public static String decryptThreeDESECB(String src,String key,String charSet) throws Exception {
		DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(charSet));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		SecretKey securekey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DESede");
		cipher.init(Cipher.DECRYPT_MODE, securekey);
		byte[] retByte = cipher.doFinal(Base64.decodeBase64(src.getBytes(charSet)));
		return new String(retByte,charSet); 
	}
	
	/**
	 * 加密过程，先用3DES进行加密，然后再用Base64进行第二次加密输出，得到密文 
	 * @param encodeByte 密文
	 * @param key 密钥
	 * @param charset 字符集
	 */
	public static String encryptThreeDESECBBYTE(String src,String key,String charSet) throws Exception {
		DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(charSet));
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
		SecretKey securekey = keyFactory.generateSecret(dks);
		Cipher cipher = Cipher.getInstance("DESede");
		cipher.init(Cipher.ENCRYPT_MODE, securekey);
//		byte[] retByte = cipher.doFinal(encodeByte);		
		byte[] retByte = cipher.doFinal(Base64.encodeBase64(src.getBytes(charSet)));
		return new String(retByte,charSet); 
	}
	 /** 
     * 3DESECB加密  
     * @param src 要进行了加密的原文 
     * @param key 密钥   key必须是长度大于等于 3*8 = 24 位 
     * @return 
     * @throws Exception 
     */  
    public static String encryptThreeDESECB(String src,String key) throws Exception  
    {  
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8"));  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");  
        SecretKey securekey = keyFactory.generateSecret(dks);  
          
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");  
        cipher.init(Cipher.ENCRYPT_MODE, securekey);  
        byte[] b=cipher.doFinal(src.getBytes());  
          
        return Base64.encodeBase64String(b);  
          
    }  
    /** 
     * 3DESECB解密 
     * @param src 要解密的密文字符 
     * @param key 解密的Key key必须是长度大于等于 3*8 = 24 位 
     * @return 
     * @throws Exception 
     */  
    public static String decryptThreeDESECB(String src,String key) throws Exception  
    {  
    	   //--通过base64,将字符串转成byte数组  
        BASE64Decoder decoder = new BASE64Decoder();  
        byte[] bytesrc = decoder.decodeBuffer(src);  
        //--解密的key  
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8"));  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");  
        SecretKey securekey = keyFactory.generateSecret(dks);  
          
        //--Chipher对象解密  
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");  
        cipher.init(Cipher.DECRYPT_MODE, securekey);  
        byte[] retByte = cipher.doFinal(bytesrc);  
          
        return new String(retByte,"utf-8");  
    } 
    public static void main(String[] args) {
    	try {
//    		String date="{\"app_version\":\"1.0.0\",\"reqType\":\"10101\",\"key\":\"CIP80101JYTX\",\"userPassword\":\"bbb\",\"userName\":\"aaa\"}";
    		String date="notifyreading";
    		String key= StringHelper.getUUID();
    		String encryptStr = encryptThreeDESECB(date,key);
			System.out.println(encryptStr);		
			encryptStr="0xnnJejDex5ThoiyyuHgOQ==";
			key="c92da3c4-1715-4d07-ace0-34036d41fb53";
    		System.out.println(decryptThreeDESECB(encryptStr,key));
//    		String des3 = "ax2Jov7XPkfq5bncyInYFwSoIfUfE6npLraTS6K377V+kqC6YI57VaqbLTMjlKR75FfgMmdyOkeESxxS5woSmBjQj6xCDmLAU8w+0m0cvwHaLdYlYS0FeQqVW6iZD5VMlAJF6q42LPw=";
//    		String key = "ea61504510763594a4bd5-31";
//    		String md5 = "29f8f3ba93560db532eeabd45498bc38";
//    		System.out.println(decryptThreeDESECB(des3,key));
//    		if(decryptThreeDESECB(des3,key).equals(date)){
//    			System.out.println("解密成功");
//    		}
//    		System.out.println(MD5Util.md5EncodeWeb(des3));
//    		if(MD5Util.md5EncodeWeb(des3).equalsIgnoreCase(md5)){
//    			System.out.println("MD5对比成功");
//    		}
//    	 	System.out.println(encryptThreeDESECB(date, key));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
}
